﻿using System;
using System.Threading;
using BusinessLogic.BusinessRules;
using BusinessLogic.ClassFactory;
using Common;
using DataCach;
using ValidateCommon;

namespace IssueCommon
{
    public class IssueManage : IssueBase, IDisposable
    {
        private SessionUser _sessionUser;

        public SessionUser SessionUser
        {
            get { return _sessionUser; }
            set { _sessionUser = value; }
        }

        public IssueManage(SessionUser sessionUser)
        {
            base.SessionUser = sessionUser;
        }

        public void IssueInclude(int intWSID)
        {
            using (CommonShtmlIssue objCommonShtmlIssue = new CommonShtmlIssue(base.SessionUser))
            {
                objCommonShtmlIssue.CreateCommonInclude(intWSID);
            }
        }

        //发布首页
        public void IssueIndex(int intWSID)
        {
            using (WebSiteFactory objFactory = new WebSiteFactory())
            {
                WebsiteIssue objWebsiteIssue = new WebsiteIssue(intWSID, base.SessionUser);
                objWebsiteIssue.IssueIndex();
            }
        }

        //发布栏目页面
        public void IssueSection(bool IsIssueAgain, int intWSID)
        {
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                Sections objSection = null;
                if (IsIssueAgain)
                {
                    objSection = objFactory.CreatorBySql("0=0 and intWSID=" + intWSID + " Order By dtmInTime Desc");
                }
                if (!IsIssueAgain)
                {
                    objSection = objFactory.CreatorBySql(string.Format("0=0 and intIssueState={0} and intWSID={1} Order By dtmInTime Desc", (int)Utility.STIssueState.NoIssue, intWSID));
                }
                objFactory.Dispose();
                if (objSection != null)
                {
                    objSection.SessionUser = this.SessionUser;
                    foreach (Model.Data.SectionsData.SectionsRow objRow in objSection.Data.Sections.Rows)
                    {
                        int ss = objRow.intSectionID;
                        ////触发多线程
                        //ParameterizedThreadStart threadDelegate = new ParameterizedThreadStart(SectionIssueThread);
                        //Thread sectionThread = new Thread(threadDelegate);
                        //sectionThread.Start(ss);
                        SectionIssueThread(ss,this.SessionUser);
                    }
                }
            }
        }

        private void SectionIssueThread(object SectionID, SessionUser objSessionUser)
        {
            if (ParamAuth.IsNumber(SectionID.ToString()))
            {
                int intSectionID = Convert.ToInt32(SectionID);
                using (SectionsFactory objFactoryThread = new SectionsFactory())
                {
                    SectionIssue objSectionIssueThread = new SectionIssue(intSectionID, SessionUser);
                    objSectionIssueThread.DoSectionIssue(objSessionUser);//发布
                    BusinessLogic.BusinessRules.Sections objSectionThread = objFactoryThread.Creator(intSectionID);
                    objSectionThread.SessionUser = base.SessionUser;
                    objSectionThread.DataItem.intIssueState = (int)Utility.STIssueState.Issue;
                    objSectionThread.Update();
                }
            }
        }

        //发布文章页面
        public void IssueArticle(bool IsIssueAgain, int intWSID)
        {
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                Sections objSection = null;
                objSection = objFactory.CreatorBySql("0=0 and intWSID=" + intWSID + " Order By dtmInTime Desc");

                if (objSection != null)
                {
                    ArticleIssue objArticleIssue;
                    foreach (Model.Data.SectionsData.SectionsRow objRow in objSection.Data.Sections.Rows)
                    {
                        objArticleIssue = new ArticleIssue(objRow.intSectionID, base.SessionUser);
                        objArticleIssue.ParentPath = objSection.DataItem.IschvParentPathNull() ? "" : objSection.DataItem.chvParentPath;
                        objArticleIssue.PageName = objSection.DataItem.IschvPageNameNull() ? "" : objSection.DataItem.chvPageName;

                        int intNoIssueArticleCount = 0;
                        ////重新发布所有文章
                        if (IsIssueAgain)
                        { intNoIssueArticleCount = objArticleIssue.GetIssueArticleCount(true); }
                        //发布未发布的文章
                        if (!IsIssueAgain)
                        { intNoIssueArticleCount = objArticleIssue.GetIssueArticleCount(false); }

                        //发布文章
                        if (intNoIssueArticleCount > 0)
                        {
                            objArticleIssue.CreateArticle(IsIssueAgain);
                        }
                    }
                }
            }
        }

        public void IssueArticle(int intWSID, int intArtSecID, int intSectionID)
        {
            using (SectionsFactory objFactory = new SectionsFactory())
            {
                Sections objSection = null;
                objSection = objFactory.CreatorBySql(string.Format("0=0 and intSectionID={0} and intWSID={1} Order By dtmInTime Desc", intSectionID, intWSID));

                if (objSection != null)
                {
                    objSection.SessionUser = SessionUser;
                    ArticleIssue objArticleIssue = new ArticleIssue(objSection.DataItem.intSectionID, intArtSecID, base.SessionUser);
                    objArticleIssue.ParentPath = objSection.DataItem.IschvParentPathNull() ? "" : objSection.DataItem.chvParentPath;
                    objArticleIssue.PageName = objSection.DataItem.IschvPageNameNull() ? "" : objSection.DataItem.chvPageName;
                    objArticleIssue.IssueArticleContent();
                }
            }
        }

        public void ClearCaches()
        {
            //清除所有缓存
            SetCachecs.SetCache.Clear();
        }
    }
}